﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.Api.Application.hqApplication.Commands.Saless;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Clientss
{
    public class ShowClientHander : IRequestHandler<ShowClientComand, Result<PageResultDto<Clients>>>
    {
        private readonly IMapper mapper;

        private readonly IBaseRepository<Clients> clientsRepository;

        public ShowClientHander(IMapper mapper, IBaseRepository<Clients> clientsRepository)
        {
            this.mapper = mapper;
            this.clientsRepository = clientsRepository;
        }
        /// <summary>
        /// 销售显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<Clients>>> Handle(ShowClientComand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<Clients>>() { Code = (int)CommonErrorCode.Search };
            var page = new PageResultDto<Clients>() { List = new List<Clients>(), PageCount = 0, TotalCount = 0 };
            var res = await (await clientsRepository.GetAll()).ToListAsync();
            page.TotalCount = res.Count();
            page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
            page.List = res.OrderBy(x => x.ClientId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Data = page;
            result.Code = (int)CommonErrorCode.Success;
            result.Msg = "显示成功";
            return result;
        }
    }
}
